package edu.gatech.wms.controllers;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import android.graphics.Bitmap;
import android.location.Address;
import edu.gatech.wms.models.DBInterface;
import edu.gatech.wms.models.Item;
import edu.gatech.wms.models.ItemType;

/**
 * 
 * All interactions with Item objects should occur through this class
 *
 */
public abstract class ItemManager implements DBInterface {
	
	/**
	 * Get a fill List of ids for every item that exists in DB
	 * @return
	 */
	public static Set<Integer> getAllItems() {
		Set<Integer> retSet = new TreeSet<Integer>();
		
		for (Item thing : dbMain.queryAllItems()) {
			retSet.add( thing.getId());
		}
		
		return retSet;
	}
	/**
	 * 
	 * @param itemType Type Integer 0 = Found 1 = LostItem 2 = DonatedItem
	 * @return List Of ItemIDs for owned Items
	 */
	public static Set<Integer> queryItemsByType(int itemType) {
		Set<Integer> retSet = new TreeSet<Integer>();
		
		for (Item thing : dbMain.queryItemsByType(itemType)) {
			retSet.add( thing.getId() );
		}
		
		return retSet;
		
	}
	/**
	 * String based Query returns all Items that the string matches properties
	 * of.
	 * @param queryString name to match in the properties of all items in DB
	 * @return Set of itemID Integers that match object
	 */
	public static Set<Integer> queryItems(String queryString) {
		
		Map<Integer,Item> allItemIds = dbMain.queryItem( queryString );
		
		return allItemIds.keySet();
	}
	
	/**
	 * Get the name of the item
	 * @param _itemID
	 * @return
	 */
	public static String getItemName(int _itemID ) {
		return dbMain.queryItem(_itemID).getName();
	}
	/**
	 * Get Description for the Item
	 * @param _itemID
	 * @return
	 */
	public static String getItemDescription(int _itemID ) {
		return dbMain.queryItem(_itemID).getDescription();
	}
	/**
	 * Get List of Item categories
	 * @param _itemID
	 * @return
	 */
	public static List<String> getItemCategories(int _itemID ) {
		return dbMain.queryItem(_itemID).getCategories();
	}
	/**
	 * Get Image for Item
	 * @param _itemID
	 * @return
	 */
	public static Bitmap getItemImage(int _itemID ) {
		return dbMain.queryItem(_itemID).getImage();
	}
	/**
	 * What Type of Item is this
	 * @param _itemID
	 * @return
	 */
	public static ItemType getItemType(int _itemID ) {
		return dbMain.queryItem(_itemID).getType();
	}
	/**
	 * Where is the Location for this Item
	 * @param _itemID ID Number Of Item
	 * @return Location of the Item
	 */
	public static Address getLocation(int _itemID ) {
		return dbMain.queryItem(_itemID).getAddress();
	}
	/**
	 * Find Who Owns This Item
	 * @param _itemID Id Number of Item
	 * @return	Owner ID of Item
	 */
	public static int getItemOwnerId(int _itemID ) {
		return dbMain.queryItem(_itemID).getOwnerId();
	}
	
	/**
	 * Create a new Item Object in Database
	 * @param name	Name Of Type
	 * @param type	Type Of Item (by ItemType Object)
	 * @param description	String Line Description of Item
	 * @param image	Picture of Item
	 * @param ownerId	Who Owns this Item
	 * @param loc	Location where Item was found or Lost
	 * @param categories	Static List of Categories for Items
	 */
	public static void createItem(String name, ItemType type, String description, Bitmap image, int ownerId, Address loc, List<String> categories) {
		 
		int id = new Random().nextInt();	// Not the best way to deal with "unique" id numbers...
		Item item = new Item(name, type, id, description, image, ownerId, loc, categories);
		dbMain.add(item); 
	}
	
	public static void createItem(String name, ItemType type, int id, String description, Bitmap image, int ownerId, Address loc, List<String> categories) {
		
		Item item = new Item(name, type, id, description, image, ownerId, loc, categories);
		dbMain.add(item); 
	}
	
	/**
	 * @param id of Item to be deleted
	 */
	public static void deleteItem(int itemID) { 
		if ( itemID != 0) {
			dbMain.remove(dbMain.queryItem(itemID));
		}
	}
	
	public static String getItemCreationDate(int _itemID) {
		return dbMain.queryItem(_itemID).getCreationDate();
	}
	/**
	 * Update the existing User by matching the ID Number
	 * @param name
	 * @param type
	 * @param id
	 * @param description
	 * @param image
	 * @param ownerId
	 * @param loc
	 * @param categories
	 */
	public static void updateItem(String name, ItemType type, int id, String description, Bitmap image, int ownerId, Address loc, List<String> categories){
		deleteItem(id);
		createItem(name, type, id, description, image, ownerId, loc, categories);
		//This could be better
	}
	
	
}
